package com.gmako.utils;
import java.util.Date;
import java.util.Random;

import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;


public class MailManagers {
	
	private static Logger logger = Logger.getLogger(MailManagers.class);
	@Autowired
	private JavaMailSender mailSender;

	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	/**
	 * 发送邮箱验证
	 * @author qifu
	 * @param username
	 * @param mailSender
	 * @param userid
	 * @return boolean
	 */
	public boolean send(String username,String mail) {
		boolean flag = false;
		try {
			String address= generateId();
			System.out.println(mailSender+"ffffffffff");
			MimeMessage msg = mailSender.createMimeMessage();
			MimeMessageHelper message = new MimeMessageHelper(msg, true,
					"UTF-8");
			message.setFrom("tanlytec_hr@163.com");
			message.setSubject("QUNJU注册验证");
			message.setTo(mail);//mail  q20080135@163.com
			if (logger.isDebugEnabled()) {
				logger.debug("send email is" + username);
			}
			message.setText("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>注册验证</title></head><body> <div style='width:789px;height:372px;border:1px solid #0672bc;margin:85px auto 0px auto;'>    	<div style='float:left;	margin:2px 0px 0px 5px;width:780px;height:33px;background:url(cid:email_verify_title) no-repeat;display:inline;font-size:14px;'>        	<span style='float:left;margin:6px 0px 0px 693px;padding:0;'><a href='http://www.gmako.com/about.do?method=helpCenter' style='color:#016DB7;text-decoration:none;'>帮助</a></span>            <span style='float:left;margin:6px 0px 0px 25px;'>" +
					"<a href='http://man250.com/user.do?method=user' style='color:#016DB7;text-decoration:none;'>登录</a></span>        </div>        <div style='float:left;margin:25px 0px 0px 62px;width:718px;font-size:18px;font-weight:bold;display:inline;'>Hi，" +
					username+"</div>        <div style='float:left;margin:22px 0px 0px 62px;width:718px;display:inline;font-size:14px;'>您好!</div>        <div style='float:left;margin:14px 0px 0px 102px;width:678px;display:inline;font-size:14px;'>请点击&nbsp;" +
					"<a href='http://www.gmako.com/sendmail.do?method=check&address="+address+"' style='color:#016DB7;text-decoration:none;'>这里</a>&nbsp;完成验证，如果无法打开请复制以下链接到你的浏览器（如IE）的地址栏完成验证</div>        <div style='float:left;margin:5px 0px 0px 102px;width:678px;display:inline;font-size:14px;'>" +
					"http://www.gmako.com/sendmail.do?method=check&address="+address+"</div>        <div style='float:left;margin:11px 0px 0px 102px;width:678px;display:inline;font-size:14px;'></div>        <div style='float:left;margin:9px 0px 0px 275px;width:505px;display:inline;font-size:14px;'></div>        <div style='float:left;margin:22px 0px 0px 62px;background:url(cid:email_verify_split) no-repeat;width:626px;height:1px;line-height:1px;display:inline;overflow:hidden;'></div>        <div style='float:left;margin:16px 0px 0px 321px;display:inline;color:#aaa9a9;'></div>    	<div style='clear:both;'></div>        <div style='float:left;margin:6px 0px 0px 289px;display:inline;color:#aaa9a9;'></div>    	<div style='clear:both;'></div>    </div></body></html>'",true);
			MailcapCommandMap mc = (MailcapCommandMap) CommandMap
					.getDefaultCommandMap();
			mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
			mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
			mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
			mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
			mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
			CommandMap.setDefaultCommandMap(mc);
			mailSender.send(msg);
			flag = true;
		} catch (MessagingException e) {
 
			e.printStackTrace();
		}
		return flag;

	}
	
	/**
	 * 忘记密码发送邮箱验证
	 * @author qifu
	 * @param username
	 * @param mail
	 * @param userid
	 * @return boolean
	 */
	public boolean sendforgetPW(String username,String mail,int userid) {
		boolean flag = false;
		try {
			String address= generateId();
			MimeMessage msg = mailSender.createMimeMessage();
			MimeMessageHelper message = new MimeMessageHelper(msg, true,
					"UTF-8");
			message.setFrom("tanlytec_hr@163.com");
			message.setSubject("QUNJU找回密码");
			message.setTo(mail);//mail  q20080135@163.com
			if (logger.isDebugEnabled()) {
				logger.debug("send email is" + username);
			}
			message.setText("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />" +
					"<title>GMAKO找回密码</title></head><body> <div style='width:789px;height:372px;border:1px solid #0672bc;margin:85px auto 0px auto;'> " +
					"   	<div style='float:left;	margin:2px 0px 0px 5px;width:780px;height:33px;background:url(cid:email_verify_title) no-repeat;display:inline;font-size:14px;'>  " +
					"      	<span style='float:left;margin:6px 0px 0px 693px;padding:0;'><a href='http://www.gmako.com/FooterHelp/footerHelpInfo.do?tId=5 style='color:#016DB7;text-decoration:none;'>帮助</a></span>" +
					"            <span style='float:left;margin:6px 0px 0px 25px;'>" +
					"<a href='http://www.gmako.com/user.do?method=user' style='color:#016DB7;text-decoration:none;'>登录</a></span>" +
					"        </div>        <div style='float:left;margin:25px 0px 0px 62px;width:718px;font-size:18px;font-weight:bold;display:inline;'>Hi，" +
					username+"</div>        <div style='float:left;margin:22px 0px 0px 62px;width:718px;display:inline;font-size:14px;'>您好，这是一封找回密码的邮件：" +
							"</div>        <div style='float:left;margin:14px 0px 0px 102px;width:678px;display:inline;font-size:14px;'>请点击&nbsp;" +
					"<a href='http://www.gmako.com/user.do?method=check&address="+address+"' style='color:#016DB7;text-decoration:none;'>这里</a>" +
							"&nbsp;进行修改密码，如果无法打开请复制以下链接到你的浏览器（如IE）的地址栏完成验证</div>        " +
							"<div style='float:left;margin:5px 0px 0px 102px;width:678px;display:inline;font-size:14px;'>" +
					"http://www.gmako.com/user.do?method=check&address="+address+"</div>        <div style='float:left;margin:11px 0px 0px 102px;width:678px;display:inline;font-size:14px;'>" +
							"验证完成后，如果您丢失密码，就可以通过邮箱找回啦！</div>        <div style='float:left;margin:9px 0px 0px 275px;width:505px;display:inline;font-size:14px;'>希望您在GMAKO度过快乐的时光！</div>        " +
							"<div style='float:left;margin:22px 0px 0px 62px;background:url(cid:email_verify_split) no-repeat;width:626px;height:1px;line-height:1px;display:inline;overflow:hidden;'></div>        " +
							"<div style='float:left;margin:16px 0px 0px 321px;display:inline;color:#aaa9a9;'>此为系统邮件，请勿回复</div>    	" +
							"<div style='clear:both;'></div>        " +
							"<div style='float:left;margin:6px 0px 0px 289px;display:inline;color:#aaa9a9;'>" +
							"请保管好你的邮箱，避免帐号被他人盗用</div>    	" +
							"<div style='clear:both;'></div>    </div></body></html>'",true);
			MailcapCommandMap mc = (MailcapCommandMap) CommandMap
					.getDefaultCommandMap();
			mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
			mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
			mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
			mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
			mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
			CommandMap.setDefaultCommandMap(mc);
			mailSender.send(msg);
			flag = true;
			if (logger.isInfoEnabled()) {
				logger.info(userid + " :userid");
			}
		} catch (MessagingException e) {
 
			if (logger.isTraceEnabled()) {
				logger.error(userid + "send error");
			}
			e.printStackTrace();
		}
		return flag;

	}



	public void projectsendmail() {
		System.out.println("diaoduss");
		try {
			MimeMessage msg = mailSender.createMimeMessage();
			MimeMessageHelper message = new MimeMessageHelper(msg, true,
					"UTF-8");
			message.setFrom("gmakoidea@163.com");
			message.setSubject("my mail send");
			message.setTo("460259412@qq.com");
			message.setText("aaaaaaaaaaa");
			mailSender.send(msg);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Send Ok!!");
	}
	/**
	 * 生成ID
	 * 
	 * @return
	 */
	public static String generateId() {
		return new Date().getTime() + getRandomString(5);
	}

	/**
	 * 生成随机字符串(数字+字母)
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) {
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(3);
			long result = 0;
			switch (number) {
			case 0:
				result = Math.round(Math.random() * 25 + 65);
				sb.append(String.valueOf((char) result));
				break;
			case 1:
				result = Math.round(Math.random() * 25 + 97);
				sb.append(String.valueOf((char) result));
				break;
			case 2:
				sb.append(String.valueOf(new Random().nextInt(10)));
				break;
			}
		}
		return sb.toString();
	}
}
